﻿using ChoMy.Models;
using ChoMy.Models.Data;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChoMy.Controllers
{
    public class ShoppingCartController : Controller
    {
        private ChoMyContext db = new ChoMyContext();

        [HttpPost]
        public ActionResult Show()
        {
            List<ShoppingCart> shoppingCarts = db.ShoppingCarts
                .Where(x => x.CreatedBy == User.Identity.Name)
                .ToList();

            return PartialView("_ShoppingCart", shoppingCarts);
        }

        [HttpPost]
        public ActionResult Add(int id)
        {
            ShoppingCart shoppingCartItem = db.ShoppingCarts
               .FirstOrDefault(x => x.CreatedBy == User.Identity.Name && x.StyleId == id);

            if (shoppingCartItem == null)
            {
                ShoppingCart newShoppingCartItem = new ShoppingCart();
                newShoppingCartItem.StyleId = id;

                db.ShoppingCarts.Add(newShoppingCartItem);
                db.SaveChanges();
            }
            else
            {
                if (shoppingCartItem.Quantity < 10)
                {
                    shoppingCartItem.Quantity = shoppingCartItem.Quantity + 1;
                }

                db.Entry(shoppingCartItem).State = EntityState.Modified;
                db.SaveChanges();
            }

            return Json(CountProductQuantity(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult Count()
        {
            return Content(CountProductQuantity().ToString());
        }

        [HttpPost]
        public ActionResult Edit(int id, int quantity)
        {
            ShoppingCart shoppingCartItem = db.ShoppingCarts
               .FirstOrDefault(x => x.CreatedBy == User.Identity.Name && x.StyleId == id);

            shoppingCartItem.Quantity = quantity;

            db.Entry(shoppingCartItem).State = EntityState.Modified;
            db.SaveChanges();

            return Json(CountProductQuantity(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            ShoppingCart shoppingCart = db.ShoppingCarts
               .FirstOrDefault(x => x.CreatedBy == User.Identity.Name && x.StyleId == id);

            db.ShoppingCarts.Remove(shoppingCart);
            db.SaveChanges();

            return Json(CountProductQuantity(), JsonRequestBehavior.AllowGet);
        }

        #region Helpers
        private int CountProductQuantity()
        {
            var quantity = db.ShoppingCarts
                .Where(x => x.CreatedBy == User.Identity.Name)
                .Select(x => x.Quantity);

            if (quantity.Any())
            {
                return quantity.Sum();
            }
            else
            {
                return 0;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }

            base.Dispose(disposing);
        }
        #endregion
    }
}